package com.example.myapplication.tack_33;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_33.adapter.CaiDanDetailsAdapter;
import com.example.myapplication.tack_33.data.CaiDanDetailsData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;

public class OrderDetailsActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView caiDan;
    private Intent intent;
    private String orderNo;
    private TextView amount1;
    private TextView orderNo1;
    private TextView receiverPhone;
    private TextView createTime;
    private TextView updateTime;
    private TextView quantity;
    private TextView amount;
    private List<CaiDanDetailsData.DataDTO.OrderInfoDTO.OrderItemListDTO> listOrderItemListDTO;
    private void init() {
        exit = findViewById(R.id.exit);
        caiDan = findViewById(R.id.caiDan);
        amount1 = findViewById(R.id.amount1);
        orderNo1 = findViewById(R.id.orderNo);
        receiverPhone = findViewById(R.id.receiverPhone);
        createTime = findViewById(R.id.createTime);
        updateTime = findViewById(R.id.updateTime);
        quantity = findViewById(R.id.quantity);
        amount = findViewById(R.id.amount);
        intent = getIntent();
        orderNo = intent.getStringExtra(Common.COMMON);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_33_activity_cai_dan_details);
        init();
        initExit();
        getDetails();
    }

    private void getDetails() {
        String url = "prod-api/api/takeout/order/"+orderNo;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, CaiDanDetailsData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CaiDanDetailsData caiDanDetailsData = (CaiDanDetailsData)obj;

                if(caiDanDetailsData.getCode() == 200){
                    listOrderItemListDTO =  caiDanDetailsData.getData().getOrderInfo().getOrderItemList();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            amount1.setText("实付金额：￥ "+caiDanDetailsData.getData().getOrderInfo().getAmount());
                            orderNo1.setText("订单号："+caiDanDetailsData.getData().getOrderInfo().getOrderNo());
                            receiverPhone.setText("手机号："+caiDanDetailsData.getData().getOrderInfo().getReceiverPhone());
                            createTime.setText("下单时间："+caiDanDetailsData.getData().getOrderInfo().getCreateTime());
                            updateTime.setText("付款时间："+caiDanDetailsData.getData().getOrderInfo().getUpdateTime());
                            quantity.setText("数量："+caiDanDetailsData.getData().getOrderInfo().getOrderItemList().get(0).getQuantity());
                            amount.setText("付款时间："+caiDanDetailsData.getData().getSellerInfo().getAvgCost());
                            caiDan.setAdapter(new CaiDanDetailsAdapter(listOrderItemListDTO, OrderDetailsActivity.this));
                            caiDan.setLayoutManager(new LinearLayoutManager(OrderDetailsActivity.this));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderDetailsActivity.this.finish();
            }
        });
    }


}